package com.java.demo.model;

/**
 * 装饰者模式
 *
 * @auther zhangwj
 * @date 2020/12/3 上午10:19
 */
public class DecoratorPattern {

    public static void main(String[] args) {
        Beverage espresso = new HouseBlend();

        CondimentDecorator mocha = new Mocha(espresso);
        System.out.println(mocha.getDescription() + ", $" + mocha.cost());
    }
}

//饮料
abstract class Beverage {

    String description = "Unknown Beverage";

    String getDescription() {
        return description;
    }

    abstract double cost();
}

// 调味品装饰
abstract class CondimentDecorator extends Beverage {

    abstract String getDescription();
}

// 浓缩咖啡
class Espresso extends Beverage {

    Espresso() {
        description = "Espresso";
    }

    @Override
    double cost() {
        return 1.99;
    }
}

// 混合咖啡
class HouseBlend extends Beverage {

    HouseBlend() {
        description = "House Blend coffee";
    }

    @Override
    double cost() {
        return 0.89;
    }
}

class Mocha extends CondimentDecorator {

    Beverage beverage;

    Mocha(Beverage beverage) {
        this.beverage = beverage;
    }

    @Override
    double cost() {
        return 0.2 + beverage.cost();
    }

    @Override
    String getDescription() {
        return beverage.getDescription() + ", Mocha";
    }
}